package com.cn.hnust.controller.hcontroller;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.cn.hnust.pojo.Lfdshop;
import com.cn.hnust.pojo.User;
import com.cn.hnust.service.ILfdshopService;
import com.cn.hnust.service.IUserService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/hlogin")
public class LoginController {

	@Resource
	private IUserService userService;

	@Autowired
	private ILfdshopService LfdshopService;

	@RequestMapping("/viewlogin")
	public String viewpaper(HttpServletRequest request, Model model) {

		return "login";
	}

	@RequestMapping("/tosign")
	public String tosign(HttpServletRequest request, Model model) {
		return "sign";
	}

	@RequestMapping("/sign")
	public String sign(@RequestParam(value = "file") MultipartFile file,HttpSession session, HttpServletResponse response, HttpServletRequest request, Lfdshop Lfdshop,
			String address, String brief, String dname, String pwd, Model model, User user, String name, String img,
			String sex, Integer age, BigDecimal price, String svip, String zaccountnum) throws IOException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// �������ڸ�ʽ
		System.out.println(df.format(new Date()));// new Date()Ϊ��ȡ��ǰϵͳʱ��
		String filePath = "E:\\wangxeclipseworkspace\\mfyy\\src\\main\\webapp\\Lfdshopimg";
		String originalFilename = file.getOriginalFilename();
		System.out.println("originalFilename:" + originalFilename);
		String newFileName = UUID.randomUUID() + originalFilename;
		File targetFile = new File(filePath, newFileName);
		file.transferTo(targetFile);
		Lfdshop.setImg("http://localhost:8080/mfyy/Lfdshopimg/" + newFileName);
		Lfdshop.setAddress(address);
		Lfdshop.setBrief(brief);
		Lfdshop.setDname(dname);
		Lfdshop.setName(name);
		Lfdshop.setPrice(price);
		Lfdshop.setPwd(pwd);
		Lfdshop.setStatus(0);
		LfdshopService.insertSelective(Lfdshop);
		return "login";

	}

	//
	@RequestMapping("/checklogin")
	public void checklogin(HttpSession session, HttpServletResponse response, HttpServletRequest request, Model model,
			Lfdshop Lfdshop, User user, String name, String pass, Integer role) throws IOException {

		System.out.println("name:" + name + " pass:" + pass);
		user = userService.selectCheckLogin(name, pass);
		String s = "";
		Lfdshop = LfdshopService.selectLfdshopByNamePwd(name, pass);
		if (role == 0) {
			if (user == null) {
				s = "0";
				Gson gson = new Gson();
				String json = gson.toJson(s);
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setHeader("Access-Control-Allow-Methods", "GET,POST");
				response.setContentType("text/json,charset=utf-8");
				response.setCharacterEncoding("utf-8");
				Writer out = response.getWriter();
				out.write(json);
				out.flush();
			} else {
				s = "1";
				session.setAttribute("username", name);
				session.setAttribute("pwd", pass);
				session.setAttribute("mfyy", user);
				session.setAttribute("role", role);
				Gson gson = new Gson();
				String json = gson.toJson(s);
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setHeader("Access-Control-Allow-Methods", "GET,POST");
				response.setContentType("text/json,charset=utf-8");
				response.setCharacterEncoding("utf-8");
				Writer out = response.getWriter();
				out.write(json);
				out.flush();
			}
		}

		if (role == 1) {
			if (Lfdshop == null) {
				s = "0";
				Gson gson = new Gson();
				String json = gson.toJson(s);
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setHeader("Access-Control-Allow-Methods", "GET,POST");
				response.setContentType("text/json,charset=utf-8");
				response.setCharacterEncoding("utf-8");
				Writer out = response.getWriter();
				out.write(json);
				out.flush();
			} else {
				s = "1";
				session.setAttribute("username", name);
				session.setAttribute("pwd", pass);
				session.setAttribute("mfyy", Lfdshop);
				session.setAttribute("role", role);
				Gson gson = new Gson();
				String json = gson.toJson(s);
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setHeader("Access-Control-Allow-Methods", "GET,POST");
				response.setContentType("text/json,charset=utf-8");
				response.setCharacterEncoding("utf-8");
				Writer out = response.getWriter();
				out.write(json);
				out.flush();
			}
		}
	}
}
